logo

# Libraries
library(tidyverse)
library(sf)
library(USAboundaries)
library(ggthemes)
library(cowplot)
library(sp)
library(leaflet)



Measurement threshold (2000 - 2020)

A minimum threshold of 10 distinct measurements with at least 1 measurement every 5 years was applied to filter for only the regularly monitored wells. investigate regularly monitored wells




### USGS SPATIAL + TIMESERIES DATAFRAMES 

# California state shape (CRS = 5070)
ca = ca_shp %>%  
  st_transform(5070)

# add 'well #' for each unique well
usgs = ca_nwis_unique_sites
usgs$wellid = paste("well", 1:nrow(usgs))

# remove wells located at same lat/long but have different well ID
usgs_spatial = usgs %>% 
  st_as_sf(coords = c('long_nad83', 'lat_nad83'), crs = 4269) %>% 
  st_transform(5070) %>%
  as_Spatial() %>% 
  remove.duplicates() %>% 
  st_as_sf() %>% 
  group_by(wellid)

# match 'well #' from spatial dataframe to corresponding well in timeseries
usgs_time = ca_nwis_all %>% 
  group_by(site_id) 
usgs_time = left_join(usgs_time, select(usgs, site_id, wellid), by = "site_id") 
usgs_time = usgs_time %>% filter(wellid %in% usgs_spatial$wellid)

# FILTER BY THRESHOLD - atleast 10 measurements and atleast 1 measurement every 5 years
thresh = usgs_time %>% 
  group_by(wellid) %>% 
  filter(measurement_dist >= 10) %>% 
  mutate(measure_period = year - lag(year)) %>% 
  filter(any(measure_period > 5))
usgs_time = usgs_time %>% filter(!wellid %in% thresh$wellid, measurement_dist >= 10) %>% 
  mutate(measure_period = year - lag(year))
usgs_spatial = usgs_spatial %>% 
  filter(!wellid %in% thresh$wellid) %>% 
  filter(measurement_dist >= 10)